package com.sheng.center.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import com.sheng.center.entity.Plan;

public class PlanDaoImpl implements PlanDao {

	private SessionFactory sessionFactory;
	@Override
	public void add(Plan plan) {
		plan.setIsDelete(0);
		sessionFactory.getCurrentSession().save(plan);
	}

	@Override
	public void edit(Plan plan) {
		plan.setIsDelete(0);
		sessionFactory.getCurrentSession().update(plan);
	}

	@Override
	public void delete(Plan plan) {
		plan.setIsDelete(1);
		sessionFactory.getCurrentSession().update(plan);
	}
	
	
	@Override
	public void planOne(Plan plan) {
		//通过审核
		plan.setIsExamine(1);
		sessionFactory.getCurrentSession().update(plan);
	}
	
	@Override
	public void planTwo(Plan plan) {
		//未通过审核
		plan.setIsExamine(2);
		sessionFactory.getCurrentSession().update(plan);
	}
	
	@Override
	public void planEnough(Plan plan) {
		//设置为人数已满
		plan.setIsEnough(1);
		sessionFactory.getCurrentSession().update(plan);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Plan> findAllList(Plan model) {
		String hql = "from Plan where 1=1  and is_delete=0";
		if(null != model.getPlanName()){
			hql = hql + " and plan_name like '%"+model.getPlanName()+"%'";
		}
		Query query = sessionFactory.getCurrentSession().createQuery(hql);  
        List list = query.list();  
        return list;
	}
	
	/**
	 * 查询全部已经通过审核的报名信息
	 */
	@Override
	public List<Plan> findAllListExamine(Plan model) {
		String hql = "from Plan where 1=1  and is_delete=0 and is_examine = 1";//1-已通过审核
		if(null != model.getPlanName()){
			hql = hql + " and plan_name like '%"+model.getPlanName()+"%'";
		}
		Query query = sessionFactory.getCurrentSession().createQuery(hql);  
        List list = query.list();  
        return list;
	}

	public Plan findById(Plan plan) {
		Query query = sessionFactory.getCurrentSession().createQuery("from Plan where id='"+plan.getId()+"'");  
		List list = query.list();  
		if (null != list && 0 < list.size()) {
			plan = (Plan) list.get(0);
		}
		return plan;
	}

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	


}
